package com.topnav.mc.admin.service.impl;

import java.util.Date;
import java.util.List;

import com.js.lib.cache.SysCache;
import com.topnav.mc.common.Constant;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.js.lib.exception.CustomException;
import com.topnav.mc.admin.dao.mapper.SystemConfigMapper;
import com.topnav.mc.admin.entity.SystemConfig;
import com.topnav.mc.admin.service.ISystemConfigService;
import com.topnav.mc.base.CommServiceImpl;
@Service
public class SystemConfigServiceImpl extends CommServiceImpl<SystemConfigMapper,SystemConfig> implements ISystemConfigService{
	@Override
	public boolean beforeSave(SystemConfig entity){
		if (this.getOne(new QueryWrapper<SystemConfig>().eq("sys_key",entity.getSysKey()), true) != null){
			throw new CustomException(-1,"Key  %s 已存在",entity.getSysKey());
		}
		return true;
	}

	@Override
	public boolean beforeUpdate(SystemConfig entity){
		QueryWrapper<SystemConfig> wrapper = new QueryWrapper<SystemConfig>()
				.eq("sys_key", entity.getSysKey()).ne("id", entity.getId());
		if (this.getOne(wrapper, true) != null){
			throw new CustomException(-1,"Key  %s 已存在",entity.getSysKey());
		}
		entity.setUpdateTime(new Date());
		return true;

	}

	@Override
	public void afterSave(SystemConfig entity){
		this.saveToCache(entity);
	};

	@Override
	public void afterUpdate(SystemConfig entity){
		this.saveToCache(entity);
	};

	@Override
	public boolean beforeDelete(SystemConfig entity){
		return true;
	}

    /**
     * 查询列表
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
	@Override
	public List<SystemConfig> list(Wrapper<SystemConfig> queryWrapper) {
		QueryWrapper<SystemConfig> wrapper = new QueryWrapper<SystemConfig>();
		if (queryWrapper.getEntity() != null){
			if (queryWrapper.getEntity().getId()!=null){
				wrapper.eq("id", queryWrapper.getEntity().getId());
			}

			if (!StringUtils.isEmpty(queryWrapper.getEntity().getSysKey())){
				wrapper.eq("sys_key", queryWrapper.getEntity().getSysKey());
			}
		}

        return getBaseMapper().selectList(wrapper);
    }

    /**
     * 翻页查询
     *
     * @param page         翻页对象
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
	@Override
	public <E extends IPage<SystemConfig>> E page(E page, Wrapper<SystemConfig> queryWrapper) {
		QueryWrapper<SystemConfig> wrapper = new QueryWrapper<SystemConfig>();
		if (queryWrapper.getEntity() != null){
			if (queryWrapper.getEntity().getId()!=null){
				wrapper.eq("id", queryWrapper.getEntity().getId());
			}
			if (!StringUtils.isEmpty(queryWrapper.getEntity().getSysKey())){
				wrapper.like("sys_key", queryWrapper.getEntity().getSysKey());
			}
		}
       return getBaseMapper().selectPage(page, wrapper);
    }

	@Override
	public void saveToCache(SystemConfig sc) {
		SysCache.getInstance().hset(Constant.CACHE_KEY_SYSTEMCONFIG, sc.getSysKey(), sc);
	}

	@Override
	public void saveAllToCache() {
		SysCache.getInstance().del(Constant.CACHE_KEY_SYSTEMCONFIG);
		List<SystemConfig> list = this.list();
		for(SystemConfig s:list){
			saveToCache(s);
		}
	}
}
